// pages/redactstyle/redactstyle.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userinfo: [],//用户信息
        ifdrag: false,//是否显示拖拽
        iflayer: false,//是否显示弹窗
        ifaddwork: false,//是否显示添加功能弹窗 
        layarr: {
            name: '简介1',
            inputValue: '',
            mark: ""
        },
        arrlist: [],//所有的数据 
        rewfunc: [],//预约打赏
        draglist: [],//拖拽数据 
        fixlist:[],//初始化所有的数据
    },
    cData: {},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        wx.setNavigationBarTitle({
            title: '名片风格编辑'
        })
        _this.getList();
        // _this.getfunctionList();
        app.getUserall(function (userData) {
            _this.setData({
                "userinfo": userData
            })
        })
       
    },

    binderrorimg: function (e) {
        // console.log("w", e);
        // var arrlist = this.data.arrlist;
        // console.log(arrlist);
        // arrlist.forEach(function(obj,i){

        // })


    },
    //获取名片列表
    getList: function () {
        wx.showLoading({
            title: '加载中',
        })
        var _this = this;
        app.http.POST("/MiniCardCustomStyle/GetMyStyleList", {}, (err, data) => {
            console.log("3333", data)
            if (data.code == 1) {
                var datalist = data.data;
                var rewfunc = [];
                var rewardFunc = null;//打赏
                var reservaFunc = null; //预约
                datalist = datalist.filter(function (obj, i) {
                    if (obj.FuncD.ServerWeek) {
                        var ServerWeek = obj.FuncD.ServerWeek;
                        ServerWeek = ServerWeek.split(",");
                        obj.FuncD.ServerWeek = ServerWeek;
                    }
                    if (obj.Func.FuncMark == "reserva") {
                        rewfunc.push(obj);
                        return false;
                    }
                    if (obj.Func.FuncMark == "reward") {
                        rewfunc.push(obj);
                        return false;
                    }
                    return true;
                })
                _this.setData({
                    "arrlist": datalist,
                    'rewfunc': rewfunc,
                })
                wx.hideLoading()
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    }, 
    touchstart: function (e) { 
        this.cData.startY = e.changedTouches[0].pageY;
    },
    touchend: function (e) { 
        var id = e.currentTarget.dataset.id,
            arr = this.data.arrlist,
            obj,
            moveY = e.changedTouches[0].pageY - this.cData.startY;  
        arr.every(function (cObj) {
            if (cObj.Func.FuncId == id) {
                obj = cObj;
                return false;
            }
            return true;
        })
        obj.Func.y = obj.Func.y + moveY;
        arr.sort(function (a, b) { 
          return a.Func.y > b.Func.y ? 1 : a.Func.y < b.Func.y?-1:0;
        });
        var i = 0;
        arr.forEach(function (obj) {
            if (obj.Func.IsShow == 1) {
                obj.Func.y = i * 50;
                i++;
            }
        });
        console.warn("arr", arr);
        this.setData({ 'arrlist': arr });
    },
    //选择拖拽，确定按钮
    dragsecle: function () {
        var _this = this;
        var ifdrag = !_this.data.ifdrag;
        var arrlist = _this.data.arrlist;
        var i = 0;
        arrlist.forEach(function (obj) {
            if (obj.Func.IsShow == 1) {
                obj.Func.y = i * 50;
                i++;
            }
        });
        _this.setData({
            'ifdrag': ifdrag,
            'arrlist': arrlist
        });
    },
    //隐藏头部
    clickdelHd: function (e) {
        console.log(e);
        var _this = this;
        var mark = e.currentTarget.dataset.mark,
            arr = _this.data.rewfunc;
        arr.forEach(function (obj, i) {
            if (obj.Func.FuncMark == mark) {
                obj.Func.IsShow = 0
            }
        })
        _this.setData({
            'rewfunc': arr
        });
    },
    //隐藏项目
    clickHide: function (e) {
        //console.log(e);
        var _this = this;
        var index = e.currentTarget.dataset.id,
            mark = e.currentTarget.dataset.mark,
            arr = _this.data.arrlist;
        console.log(index);
        var storeArr = _this.data.storeArr;
        var piackArr = this.data.piackArr;
        arr.forEach(function (obj, i) {
            if (obj.Func.FuncMark == mark) {
                obj.Func.IsShow = 0
            }
        })
        _this.setData({
            'arrlist': arr
        });
    },

    //添加功能
    piackaddwork: function (e) {
        var _this = this;
        var mark = e.currentTarget.dataset.mark,
            isshow = e.currentTarget.dataset.isshow,
            arrlist = _this.data.arrlist,
            arrhd = _this.data.rewfunc;

        arrlist.forEach(function (obj, i) {
            if (obj.Func.FuncMark == mark) {
                if (obj.Func.IsShow == 1) {
                    obj.Func.IsShow = 0
                } else {
                    obj.Func.IsShow = 1
                }
            }
        });
        arrhd.forEach(function (obj, i) {
            if (obj.Func.FuncMark == mark) {
                if (obj.Func.IsShow == 1) {
                    obj.Func.IsShow = 0
                } else {
                    obj.Func.IsShow = 1
                }
            }
        });
        _this.setData({
            "arrlist": arrlist,
            "rewfunc": arrhd
        })
    },
    //关闭弹窗
    clerlayer: function () {
        this.setData({ 'iflayer': false });
    },
    //关闭添加功能弹窗
    cleraddwork: function () {
        this.setData({ 'ifaddwork': false });
    },
    //点击编辑弹出
    compile: function (e) {
        var title = e.currentTarget.dataset.title,
            mark = e.currentTarget.dataset.mark;
        this.setData({ 'iflayer': true });
        this.setData(
            {
                "layarr.name": title,
                "layarr.mark": mark
            }
        )
    },
    //点击添加弹出
    addpack: function () {
        this.setData({ 'ifaddwork': true });
    },
    //输入内容
    bindKeyInput: function (e) {
        this.setData({
            "layarr.inputValue": e.detail.value
        })
    },
    //确定修改标题
    clickUpdata: function () {
        var val = this.data.layarr.inputValue,
            name = this.data.layarr.name,
            mark = this.data.layarr.mark,
            arrlist = this.data.arrlist;
        arrlist.forEach(function (obj, i) {
            if (obj.Func.FuncMark == mark) {
                obj.Func.FuncTitle = val;
            }
        })
        this.setData({
            'arrlist': arrlist,
            'iflayer': false
        });
    },
    //点击保存
    butsave: function () {
        var rewfunc = this.data.rewfunc;
        var arrlist = this.data.arrlist;
        var arr = new Array;
        rewfunc.forEach(function (obj, i) { 
                arr.push(obj.Func); 
        })
        arrlist.forEach(function (obj, i) { 
                arr.push(obj.Func); 
        })
        console.log(arr)
        var json_str = JSON.stringify(arr);
        console.log(json_str);
        app.http.POST("/MiniCardCustomStyle/UpdateMyFuncSet", { "json_str": json_str }, (err, data) => {
            console.log(data);
            if (data.code == 1) {

                wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: '保存失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var userInfo = app.globalData.userInfo;
        //判断当前用户是否开通服务,开通则分享美名片页,否则分享访问记录页
        if (userInfo == null || typeof (userInfo) == "undefined") {
            app.getUserall(function (res) {
                userInfo = res;
            });
        }
        var path = userInfo.UserType == 2 ? "/pages/index/index?cid=" + userInfo.Id : "/pages/cardrecord/cardrecord";
        return {
            title: userInfo.NickName + "的美名片",
            path: path,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: "success",
                    duration: 200
                })
            }
        }
    }

})